package com.github.linkoog.model_designer.ide.schema;

import com.github.linkoog.model_designer.ide.type.ModelYamlFileType;
import com.github.linkoog.model_designer.ide.type.TableYamlFileType;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider;
import com.jetbrains.jsonSchema.extension.JsonSchemaImportedProviderMarker;
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory;
import com.jetbrains.jsonSchema.extension.SchemaType;
import com.jetbrains.jsonSchema.impl.JsonSchemaVersion;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class LocalJsonSchemaProviderFactory implements JsonSchemaProviderFactory, DumbAware {

    @Override
    public List<JsonSchemaFileProvider> getProviders(@NotNull Project project) {
        return Arrays.asList(
                new MyProvider("DBTable", "/schema/DBTable.json", virtualFile -> virtualFile.getFileType() instanceof TableYamlFileType),
                new MyProvider("DBModel", "/schema/DBModel.json", virtualFile -> virtualFile.getFileType() instanceof ModelYamlFileType)
        );
    }

    public static class MyProvider implements JsonSchemaFileProvider, JsonSchemaImportedProviderMarker{
        private String name;
        private String schemaPath;
        private Predicate<VirtualFile> availablePredicate;

        public MyProvider(@NotNull String name,@NotNull String schemaPath,@NotNull Predicate<VirtualFile> availablePredicate){
            this.name = name;
            this.schemaPath = schemaPath;
            this.availablePredicate = availablePredicate;
        }

        @Override
        public boolean isAvailable(@NotNull VirtualFile file) {
            return availablePredicate.test(file);
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public VirtualFile getSchemaFile() {
            return JsonSchemaProviderFactory.getResourceFile(LocalJsonSchemaProviderFactory.class, this.schemaPath);
        }

        @Override
        public SchemaType getSchemaType() {
            return SchemaType.schema;
        }

        @Override
        public JsonSchemaVersion getSchemaVersion() {
            return JsonSchemaVersion.SCHEMA_7;
        }
    }
}
